package cn.burnit.utils;


import cn.burnit.pojo.Reader;
import cn.burnit.service.ReaderService;

import javax.swing.table.AbstractTableModel;
import java.util.List;

/**
 * 读者表格数据
 */
public class ReaderTableModel extends AbstractTableModel {


    String[] columnNames = new String[] { "读者编号", "读者姓名", "读者类型", "读者性别","可借书数","可借天数" };

    // 使用从ReaderService返回的List作为TableModel的数据

    public List<Reader> readers;

    {
        ReaderService readerService = new ReaderService();
        readers = readerService.list();
        for (Reader reader : readers) {
            System.out.println(reader);
        }
    }

    // 返回一共有多少行
    @Override
    public int getRowCount() {
        //System.out.println(readers.size());
        return readers.size();
    }

    // 返回一共有多少列
    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    // 获取每一列的名称
    @Override
    public String getColumnName(int columnIndex) {

        return columnNames[columnIndex];
    }

    // 单元格是否可以修改
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        return false;
    }

    // 先通过readers.get(rowIndex)获取行对应的Reader对象
    // 然后根据columnIndex返回对应的属性
    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Reader reader = readers.get(rowIndex);
        if (0 == columnIndex){
            return reader.getId();
        }

        if (1 == columnIndex){
            return reader.getReadername();
        }

        if (2 == columnIndex){
            return reader.getReadertype();
        }

        if (3 == columnIndex){
            return reader.getSex();
        }
        if (4 == columnIndex){
            return reader.getMax_num();
        }
        if (5 == columnIndex){
            return reader.getDays_num();
        }
        return null;
    }

}
